package com.bdy.lm.taximanager.ui;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.bdy.lm.taximanager.R;
import com.bdy.lm.taximanager.app.MyApp;
import com.bdy.lm.taximanager.http.HttpJson;
import com.bdy.lm.taximanager.model.MessagePackaging;
import com.bdy.lm.taximanager.model.Taxi;
import com.bdy.lm.taximanager.util.JsonUtils;
import com.bdy.lm.taximanager.util.ToastUtils;

import org.json.JSONObject;

public class SettingActivity extends AppCompatActivity {

    private EditText license, isu;
    private Button set;
    public static final String TAXI_INFO_SP = "TaxiInfo", ALL_INFO = "allInfo";
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        license = (EditText) findViewById(R.id.set_license);
        isu = (EditText) findViewById(R.id.set_isu_flag);
        set = (Button) findViewById(R.id.set);

        set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String l = license.getText().toString();
                String i = isu.getText().toString();
                if (!l.equals("")) {

                    ToastUtils.showToast("设置成功！");
                } else if (!i.equals("")) {
                    ToastUtils.showToast("设置成功！");
                } else {
                    ToastUtils.showToast("请输入车牌号或ISU标志");
                }
            }
        });

        handler = new Handler() {
            public void handleMessage(Message msg) {
                if (msg.what == 101) {
//                    setTextView();
//                    progressDialog.dismiss();
                } else if (msg.what == 102) {
//                    progressDialog.setIndeterminate(true);
//                    progressDialog.setMessage("正在查询，稍候...");
//                    progressDialog.show();
//                    progressDialog.setCanceledOnTouchOutside(true);
                }
            }
        };
    }

//    private void getTaxiInfo(){
//        if (sp.contains(ALL_INFO)) {
//            MyApp.taxi = JsonUtils.gson.fromJson(sp.getString(ALL_INFO, ""), Taxi.class);
//
//        } else {
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    JSONObject jo = JsonUtils.mapToJsonObject(new MessagePackaging(116).getHeartBitMessagePackageMap());
//                    while (true) {
//                        if (httpJson.getPostJson() == null) {
//                            httpJson.HttpJsonPost(jo, MyApp.serverUrl);
//                        } else {
//                            MyApp.taxi = JsonUtils.gson.fromJson(httpJson.getPostJson().toString(), Taxi.class);
//                            handler.sendEmptyMessage(101);
//                            break;
//                        }
//                        delay(1000);
//                    }
//                }
//            }).start();
//        }
//    }

    private void delay(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
